package com.itheima.d7_thread_pool;

import java.util.concurrent.*;

/**
 * 目标：掌握线程池的创建。
 * |---线程池执行Callable任务执行
 */
public class ThreadPoolTest2 {
    public static void main(String[] args) throws Exception {
        // 1、通过ThreadPoolExecutor创建一个线程池对象。
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()
        );
        final MyCallable myCallable = new MyCallable(100);
        final Future<Integer> submit = pool.submit(myCallable);
        System.out.println(submit.get());
    }

}
class MyCallable implements Callable<Integer>{
            int n;

    public MyCallable(int n) {
        this.n = n;
    }

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum+=i;
        }
        return sum;
    }
}
